﻿var itemDisplay = 1;
var page = 1;
var maxItem = 2;

$(function () {
    for (var i = 0; i < itemDisplay; i++) {
        $('#multiple_question .item').eq(i).css('display', 'block');
    }
    $('#btnNext').click(function () {
        if (page * itemDisplay < maxItem) {
            //kiem tra da lam trang nay hay chua
            var isCheck = true;
            for (var i = (page - 1) * itemDisplay; i < page * itemDisplay; i++) {
                var $element = $('#multiple_question .question .item:eq(' + i + ') input:checked ');
                var check = $element.val();
                if (check == undefined)
                    isCheck = false;
                else
                    isCheck = true;
            }
            if (isCheck) {
                page += 1;
                $('#multiple_question .item').css('display', 'none');
                for (var i = (page - 1) * itemDisplay; i < page * itemDisplay; i++) {
                    $('#multiple_question .item').eq(i).css('display', 'block');
                }
                if ((page * itemDisplay) == maxItem) {
                    $('#multiple_question #btnFish').show();
                    $('#multiple_question #btnNext').hide();
                }
            }
            else
                alert("Bạn chưa trả lời hết câu hỏi trong phần này.");
        }
    })
    $('#btnPrev').click(function () {
        if (page >= 2) {
            page -= 1;
            $('#multiple_question .item').css('display', 'none');
            for (var i = (page - 1) * itemDisplay; i < page * itemDisplay; i++) {
                $('#multiple_question .item').eq(i).css('display', 'block');
            }
        }
    })
    $('#multiple_question #btnFish').click(function () {
        //kiem tra da lam trang nay hay chua
        var isCheck = true;
        for (var i = (page - 1) * itemDisplay; i < page * itemDisplay; i++) {
            var aaa = $('#multiple_question .question .item:eq(' + i + ') input:checked ').val();
            if (aaa == undefined)
                isCheck = false;
            else
                isCheck = true;
        }
        if (!isCheck) {
            alert("Bạn chưa trả lời hết câu hỏi trong phần này.");
            return;
        }

        var answerList = $('#multiple_question input:radio:checked')
        var arrayAnswer = "";
        $.each(answerList, function (index, item) {
            if (index == 0)
                arrayAnswer += $(item).val();
            else
                arrayAnswer += "|" + $(item).val();
        })
        var fullName = $('#txtFullName').val();
        var email = $('#txtEmail').val();
        var phone = $('#txtPhone').val();
        var job = $('#txtJob').val();
        $.ajax({
            type: "POST",
            url: "MultipleChoice.aspx/CheckCorrect",
            data: JSON.stringify({ answerList: arrayAnswer, fullName: fullName, email: email, phone: phone, job: job }),
            contentType: "application/json; charset=utf-8",
            async: false,
            dataType: "json",
            success: function (response) {
                $('#multiple_question .question').hide();
                $('#multiple_question .result').show();
                $('#lvl_result').html(response.d);
            },
            error: function (xhr, ajaxOptions, thrownError) {

            }
        });
    })
    $('#multiple_question #btnStart').click(function () {
        //kiem tra validation
        $('#multiple_question .information').hide();
        $('#multiple_question .question').show();
    })

})
